package com.ysl.study.designpattern.createmode.prototype;

import java.util.ArrayList;
import java.util.List;

/**
 * 原型模式测试
 *
 * @author yangshanlin
 * @date 2023/2/22 11:29
 * @describe
 */
public class PrototypeTest {

    public static void main(String[] args) {
//        shallowCopy();
        deepCopy();
    }

    /**
     * 浅拷贝
     */
    public static void shallowCopy(){
        List<String> list = new ArrayList<>();
        list.add("看书");
        list.add("画画");

        User user = new User();
        user.setNickname("张三");
        user.setAge(12);
        user.setSex("男");
        user.setHobbyList(list);

        User user2 = user.clone();
        user2.setNickname("王五");
        user2.setAge(13);
        user2.getHobbyList().add("打球");

        System.out.println("原对象：" + user);
        System.out.println("拷贝对象：" + user2);

    }

    public static void deepCopy(){
        List<String> list = new ArrayList<>();
        list.add("看书");
        list.add("画画");

        User user = new User();
        user.setNickname("张三");
        user.setAge(12);
        user.setSex("男");
        user.setHobbyList(list);

        User user2 = user.deepClone();
        user2.setSex("女");
        user2.setNickname("李四");
        user2.setAge(13);
        user2.getHobbyList().add("打球");

        System.out.println("原对象：" + user);
        System.out.println("拷贝对象：" + user2);
    }
}
